/**
 * @description 挂载全局变量、方法
 */

import produce from 'immer';
import { shallowRef } from 'vue'

export default {
    install(app: any): void {
        app.config.globalProperties.useImmer = <T extends Object>(baseState: T) => {
            // 赋予响应式
            const state = shallowRef(baseState);
            // 使用 produce 维护不可变数据
            const update = <K extends (...arg: any) => any>(updater: K) => {
                state.value = produce(state.value, updater)
            }
            return [state, update]
        };
    }
}